<?php
/**
 * @Author: CaiHongxuan
 * @Date:   2016-03-18 13:14:44
 * @Last Modified by:   CaiHongxuan
 * @Last Modified time: 2016-03-19 13:12:09
 */


	/**
	 * 回复关注subscribe事件
	 * @param  [type] $postObj [description]
	 * @param  [type] $content [description]
	 * @return [type]          [description]
	 */
	function responseSubscribe($postObj, $content){
		$toUsername   = $postObj->FromUserName;
	    $fromUsername = $postObj->ToUserName;
	    $time         = time();
	    $msgType      = 'text';
	    $template     = "<xml>
	                    <ToUserName><![CDATA[%s]]></ToUserName>
	                    <FromUserName><![CDATA[%s]]></FromUserName>
	                    <CreateTime>%s</CreateTime>
	                    <MsgType><![CDATA[%s]]></MsgType>
	                    <Content><![CDATA[%s]]></Content>
	                    <FuncFlag>0</FuncFlag>
	                    </xml>";
	    $resultStr = sprintf($template, $toUsername, $fromUsername, $time, $msgType, $content);
	    return $resultStr;
	}

	/**
	 * 回复文本消息
	 * @param  [type] $postObj [description]
	 * @param  [type] $content [description]
	 * @return [type]          [description]
	 */
	function responseText($postObj, $content){
		$toUsername   = $postObj->FromUserName;
        $fromUsername = $postObj->ToUserName;
        $time         = time();
		$msgType      = "text";
        $template     = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[%s]]></MsgType>
                        <Content><![CDATA[%s]]></Content>
                        </xml>";
		$resultStr = sprintf($template, $toUsername, $fromUsername, $time, $msgType, $content);
        return $resultStr;
	}

	/**
	 * 回复图片消息
	 * @param  [type] $postObj [description]
	 * @param  [type] $mediaId [description]
	 * @return [type]          [description]
	 */
	function responseImage($postObj, $mediaId){
		$toUsername   = $postObj->FromUserName;
		$fromUsername = $postObj->ToUserName;
		$time         = time();
		$msgType      = "image";
        $template     = "<xml>
						<ToUserName><![CDATA[%s]]></ToUserName>
						<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA[%s]]></MsgType>
						<Image>
						<MediaId><![CDATA[%s]]></MediaId>
						</Image>
						</xml>";
		$resultStr  = sprintf($template, $toUsername, $fromUsername, $time, $msgType, $mediaId);
        return $resultStr;
	}


	/**
	 * 创建自定义菜单
	 * @return [type] [description]
	 */
	function createMenu($appid, $secret, $button, $matchrule = NULL){
		$url          = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret;
		$output       = https_request($url);
		$jsoninfo     = json_decode($output, true);
		// 获取access_token凭证
		$access_token = $jsoninfo['access_token'];

		// foreach ($button as $item) {
		// 	foreach ($item as $k => $v) {
		// 		if(is_array($v)){
		// 			foreach ($item[$k] as $subitem) {
		// 				foreach ($subitem as $k2 => $v2) {
		// 					$subitem[$k2] = urlencode($v2);
		// 				}
		// 			}
		// 		}else{
		// 			$item[$k] = urlencode($v);
		// 		}
		// 	}
		// }

		// if(isset($matchrule) && !is_null($matchrule)){
		// 	foreach ($matchrule as $k => $v) {
		// 		$matchrule[$k] = urlencode($v);
		// 	}
		// 	$data = urldecode(json_encode(array('button' => $button, 'matchrule' => $matchrule)));
		// 	$url  = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $access_token;
		// }else{
		// 	$data = urldecode(json_encode(array('button' => $button)));
			$url  = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $access_token;
		// }
		// $res = https_request($url, $data);
		$res = https_request($url, $button);
		return json_decode($res, true);
	}

	/**
	 * 当$data为null时获取access_token凭证；当$data不为null时，创建自定义菜单
	 * @param  [type] $url  [description]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	function https_request($url,$data = null){
	    $curl = curl_init();
	    curl_setopt($curl, CURLOPT_URL, $url);
	    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
	    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
	    if (!empty($data)){
	        curl_setopt($curl, CURLOPT_POST, 1);
	        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
	    }
	    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	    $output = curl_exec($curl);
	    curl_close($curl);
	    return $output;
	}
